<template>
	<div class="menu">
		<el-card class="box-card">
      <el-button type="success" @click="$router.push({ name: 'menu-create' })">添加菜单</el-button>
			
			<el-table :data="menus" style="width: 100%;margin-top:20px;" border>
				<el-table-column label="编号" type="index" min-width="150">
				</el-table-column>
				<el-table-column prop="name" label="菜单名称" min-width="150">
				</el-table-column>
				<el-table-column prop="level" label="菜单级数" min-width="150">
				</el-table-column>
				<el-table-column prop="icon" label="前端图标" min-width="150">
				</el-table-column>
				<el-table-column prop="orderNum" label="排序" min-width="150">
				</el-table-column>
				<el-table-column label="操作" min-width="100">
					<template slot-scope="scope">
						<el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
						<el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
	</div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getAllMenus, deleteMenu } from '@/services/menu'
export default Vue.extend({
  name: 'MenuIndex',
  data () {
    return {
      menus: []
    }
  },
  methods: {
    async loadAllMenus () {
      const { data } = await getAllMenus()
      if (data.code === '000000') {
        this.menus = data.data
      }
    },
    handleEdit (item: any) {
      this.$router.push({
        name: 'menu-edit',
        params: {
          id: item.id
        }
      })
    },
    handleDelete (item: any) {
      this.$confirm('确认删除吗?', '删除提示', {}).then(async () => {
        const { data } = await deleteMenu(item.id)
        if (data.code === '000000') {
          this.$message.success('删除成功')
          this.loadAllMenus()
        }
      })
    }
  },
  created () {
    this.loadAllMenus()
  }
})
</script>

<style lang="scss"></style>
